package com.ama.utils;

import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class JDBCutils {
    private static String driverManager = null;
    private static String url =  null;
    private static String username =null;
    private static String password  =null;
    static {
        try{
            InputStream in = JDBCutils.class.getClassLoader().getResourceAsStream("Jdbc.properties");
            Properties properties = new Properties();
            properties.load(in);
            driverManager = (String)properties.get("driverManager");
            url = (String)properties.get("url");
            username = (String)properties.get("username");
            password = (String)properties.get("password");
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static Connection getConnection(){
        Connection connection = null;
        try{
            Class.forName(driverManager);
            connection = DriverManager.getConnection(url,username,password);
            return connection;
        }catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException();
        }
    }
    public static void relieve(Connection con ,PreparedStatement prst , ResultSet rs){
        try{
            if(con !=null) {
                con.close();
            }
            if(prst!=null){
                prst.close();
            }
            if(rs!=null){
                rs.close();
            }
        }catch (SQLException e){
            e.printStackTrace();
        }
    }

    public static void relieve(Connection con ,PreparedStatement prst){
        try{
            if(con !=null) {
                con.close();
            }
            if(prst!=null){
                prst.close();
            }
        }catch (SQLException e){
            e.printStackTrace();
        }
    }

}
